//
//  BJFMDBCommentManager.m
//  BowJoy
//
//  Created by 黄力力 on 2024/5/31.
//

#import "BJFMDBCommentManager.h"

@implementation BJFMDBCommentManager
+ (BJFMDBCommentManager *)sharedManager{
    static BJFMDBCommentManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];

    });
    return manager;
}


- (void)creatDatabase{
    //数据库在沙盒中的路径
      NSString * fileName =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:@"commentOfFMDB.sqlite"];
      
      //创建数据库
      self.database = [[FMDatabase alloc]initWithPath:fileName];
      
      //打开数据库
      if ([self.database open]) {
//          NSLog(@"打开数据库成功");
          //创建表 返回值为BOOL
       
          BOOL flag = [self.database executeUpdate:@"create table if not exists t_commentOfFMDB (id integer primary key autoincrement,userId text,followNum integer,voiceTime integer,voiceFileName text,isVoice integer)"];
          if (flag) {
              NSLog(@"成功建表");
          }else{
              NSLog(@"失败建表");
          }
          
      //关闭数据库
          [self.database close];
          
      }else{
//          NSLog(@"打开数据库失败");
      }
}


-(void)insertWitModel:(BJAnchorWallModelData *)model{
    if ([self.database open]) {
        //插入数据
        //1.executeUpdate:不确定的参数用？来占位（后面参数必须是oc对象，；代表语句结束）
//        BOOL result = [_db executeUpdate:@"INSERT I NTO t_student (name, age, sex) VALUES (?,?,?)",@"name",@(12),@"24"];
        //2.executeUpdateWithForamat：不确定的参数用%@，%d等来占位 （参数为原始数据类型，执行语句不区分大小写）//
        BOOL result = [self.database executeUpdateWithFormat:@"insert into t_commentOfFMDB (userId,followNum,voiceTime,voiceFileName,isVoice) values (%@,%ld,%ld,%@,%d)",model.userId,(long)model.followNum,(long)model.voiceTime,model.voiceFileName,model.isVoice ? 1:0];
        if (result) {
            NSLog(@"插入成功");
        }else{
            NSLog(@"插入失败");
        }
          
      }
      [self.database close];
}

// 查询数据

-(NSArray *)queryWithUserId:(NSString *)userId{
    NSMutableArray *dataList = [[NSMutableArray alloc]init];
    if ([self.database open]) {
            //返回查询数据的结果集
            FMResultSet * resultSet = [self.database executeQuery:[NSString stringWithFormat:@"select * from t_commentOfFMDB where userId = %@",userId]];
      
        while ([resultSet next]) {
            BJAnchorWallModelData *model = [[BJAnchorWallModelData alloc]init];
            model.userId = [resultSet stringForColumn:@"userId"];
            model.followNum = [resultSet intForColumn:@"followNum"];
            model.voiceTime = [resultSet intForColumn:@"voiceTime"];
            model.voiceFileName = [resultSet stringForColumn:@"voiceFileName"];
            model.isVoice = [resultSet intForColumn:@"isVoice"]  == 1?YES:NO;
            [dataList addObject:model];
        }
        
                        
    }
    [self.database close];
    return dataList;

}

// 删除数据
- (void)deleteData {
    if ([self.database open]) {
        NSString *sql = @"delete from t_commentOfFMDB";
        BOOL res = [self.database executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete db data");
        } else {
            NSLog(@"success to delete db data");
        }
        [self.database close];
    }
}
- (void)deleteDataWithUserId:(NSString *)userId {
    if ([self.database open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from t_commentOfFMDB where userId = %@",userId];
        BOOL res = [self.database executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete db data");
        } else {
            NSLog(@"success to delete db data");
        }
        [self.database close];
    }
}

@end
